package com.xsmart.oa.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.DistributorDao;
import com.xsmart.oa.dao.DistributorManageDAO;
import com.xsmart.oa.dao.SystemParamConfigDAO;
import org.apache.ibatis.annotations.Update;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/*
* 分销商管理页面
*
* @author xiaofeng
* @data 2018/2/6
* */
@Service
public class DistributorManageService {
    @Autowired
    DistributorManageDAO distributorManageDAO;

    /*
    * 查询分销商
    *
    * author xiaofeng
    */
    public ResponseBean queryDistributor(RequestBean requestBean){
        ResponseBean responseBean=requestBean.getResponseBean();
        int currentPage=requestBean.getRequestParamInteger("currentPage",1);
        int pageSize=requestBean.getRequestParamInteger("pageSize",10);
        String distributorPhone=requestBean.getRequestParamString("distributorPhone");
        String distributorName=requestBean.getRequestParamString("distributorName");
        Page<Map<String,Object>> page=new Page<>(currentPage,pageSize);
        page.setRecords(distributorManageDAO.queryDistributor(page,distributorPhone,distributorName));
        responseBean.addResultPageItem("distributorManage",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 修改分销商信息
     *
     * @author xiaofeng
     * @param requestBean 请求对象
     * @return 绑定结果
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean updateDistributor(RequestBean requestBean){
        ResponseBean responseBean=requestBean.getResponseBean();
        Integer userId=requestBean.getRequestParamInteger("user_id");
        String phone=requestBean.getRequestParamString("phone");
        Integer isValidated=requestBean.getRequestParamInteger("is_validated");
        //更新infs_user表
        try{
            int flag=distributorManageDAO.updateDistributor(userId,isValidated);
        }catch(Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }
        //更新infu_user_request_type_task表
        try{
            int flag=distributorManageDAO.updateRequestInfo(userId,phone);
        }catch(Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 修改店铺信息
     *
     * @Author xxf
     * @Date 2018/3/5
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean editShopInfo(RequestBean requestBean){
        ResponseBean responseBean=requestBean.getResponseBean();
        Map<String,Object> shopInfo=new HashMap<>();
        String Params[][]=new String[][]{
                {"shop_id",""},
                {"shop_name",""},
                {"shop_title",""},
                {"shop_address",""},
                {"seller_email",""},
                {"mobile",""},
                {"notice",""},
                {"status",""},
                {"kf_ww",""},
                {"kf_qq",""},
                {"kf_tel",""},
        };
        for (String[] Param :Params){
            Object object=requestBean.getRequestParam(Param[0]);
            shopInfo.put(Param[0],object);
        }
        try{
            distributorManageDAO.updateShopInfo(shopInfo);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

}
